﻿using CardRegisterCollection.BLL;
using CardRegisterCollection.Utility.Enum;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using _9jCardRegisterCollection.Utils;
using Menu = CardRegisterCollection.BLL.Menu;

namespace _9jCardRegisterCollection
{
    public partial class FrmLogin : AntdUI.Window
    {
        /// <summary>
        /// 当前窗体
        /// </summary>
        private readonly AntdUI.Window window;
        /// <summary>
        /// init
        /// </summary>
        public FrmLogin()
        {
            window = this;
            InitializeComponent();
        }


        private User userDal = new User();



        /// <summary>
        /// 检测用户输入是否合法
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            return Validation.CheckEmpty(window, this.txtUserName, "请输入账号")
                   && Validation.CheckEmpty(window, this.txtPassword, "请输入密码");
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butLogin_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
            Login();
        }



        /// <summary>
        /// 登录
        /// </summary>
        private void Login()
        {
            string userName = txtUserName.Text.Trim();
            string password = txtPassword.Text.Trim();

            var result = userDal.Login(userName, password);
            switch (result)
            {
                case LoginRltEnum.Normal:
                    this.DialogResult = DialogResult.OK;
                    break;
                case LoginRltEnum.Invalid:
                    AntdUI.Message.error(window, "账号不存在", autoClose: 3);
                    break;
                case LoginRltEnum.PasswordError:
                    AntdUI.Message.error(window, "密码错误", autoClose: 3);
                    break;
                case LoginRltEnum.Unauthorized:
                    AntdUI.Message.error(window, "此账户未授权，请联系管理员", autoClose: 3);
                    break;
                default:
                    break;
            }
        }
    }
}
